/**
 * 
 */
package precision;

/**
 * Floating point representation of FPObj
 * @author Alex J. Bush, Richard J. Inskip
 *
 */
public class Floats implements FPObj {

	private float var;

	/**
	 * Construct a new float precision object for use with the FPObj interface
	 */
	public Floats()
	{
		var = 0;
	}

	/**
	 * Construct a new float precision object for use with the FPObj interface
	 * @param value The initial value of this object
	 */
	public Floats(float value)
	{
		var = value;
	}

	/**
	 * Ensure both objects are of the same class
	 * @param b FPObj to test <cod>this</code> against
	 */
	private void checkClass(FPObj b)
	{
		if ( this.getClass() != b.getClass() )
		{
			throw new RuntimeException("Mismatched FPObj types; was expecting " + this.getClass() +
					" and recieved " + b.getClass()+ ".");
		}
	}

	/**
	 * Return the internal floating point number this object represents
	 * @return
	 */
	public float getValue()
	{
		return var;
	}

	@Override
	public FPObj add(FPObj b) {
		checkClass(b);
		return new Floats(var + ((Floats)b).getValue());
	}

	@Override
	public FPObj divide(FPObj b) {
		checkClass(b);
		return new Floats(var / ((Floats)b).getValue());
	}

	@Override
	public FPObj multi(FPObj b) {
		checkClass(b);
		return new Floats(var * ((Floats)b).getValue());
	}

	@Override
	public boolean greaterThan(FPObj b){
		checkClass(b);
		boolean test=false;
		if(this.getValue()>((Floats)b).getValue()){
			test=true;
		}
		return test;
	}

	@Override
	public FPObj sub(FPObj b) {
		checkClass(b);
		return new Floats(var - ((Floats)b).getValue());
	}

	@Override
	public float toFloat() {
		return var;
	}
	
	@Override
	public String toString(){
		return (""+var);
	}

	@Override
	public FPObj newFPObj() {
		return new Floats();
	}
	
	@Override
	public FPObj newFPObj(int a) {
		return new Floats((float)a);
	}

	@Override
	public FPObj abs() {
		return new Floats(Math.abs(var));
	}

	@Override
	public String toType(){
		return "Float";
	}
	
}
